﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;

namespace OrionBanque.Classe.SQLite
{
    public class Lien
    {
        
        static public Classe.Lien Charge(Int32 id)
        {
            Classe.Log.Logger.Debug("Debut Lien.Charge(" + id + ")");
            Classe.Lien p = new Classe.Lien();
            try
            {
                SQLiteCommand cmd = new SQLiteCommand(Classe.SQLite.Sql.LIEN_ID, SQLite.Sql.GetConnection());

                cmd.Prepare();
                cmd.Parameters.AddWithValue("@id", id);
                Classe.Log.Logger.Debug("Execution requete : " + SQLite.Sql.LIEN_ID);
                SQLiteDataReader rdr = cmd.ExecuteReader();
                if (rdr.Read())
                {
                    p.Id = rdr.GetInt32(0);
                    p.TypeLien = rdr.GetString(1);
                    p.Origine = rdr.GetInt32(2);
                    p.Destination = rdr.GetInt32(3);
                }
                rdr.Close();
            }
            catch (SQLiteException ex)
            {
                Classe.Log.Logger.Error(ex.Message);
                throw new Exception(String.Format(Classe.SQLite.Messages.SQLite_ERROR_GENERAL, ex.ErrorCode, ex.Message));
            }
            Classe.Log.Logger.Debug("Fin Lien.Charge()");
            return p;
        }

        static public Classe.Lien ChercheTypeOrigineOuDestination(string TypeLien, int id)
        {
            Classe.Log.Logger.Debug("Debut Lien.ChercheTypeOrigineOuDestination(" + TypeLien + "," + id + ")");
            Classe.Lien p = new Classe.Lien();
            try
            {
                SQLiteCommand cmd = new SQLiteCommand(Classe.SQLite.Sql.LIEN_TYPE_ORIGINE_OU_DESTINATION, SQLite.Sql.GetConnection());

                cmd.Prepare();
                cmd.Parameters.AddWithValue("@typelien", TypeLien);
                cmd.Parameters.AddWithValue("@origine", id);
                cmd.Parameters.AddWithValue("@destination", id);
                Classe.Log.Logger.Debug("Execution requete : " + SQLite.Sql.LIEN_TYPE_ORIGINE_OU_DESTINATION);
                SQLiteDataReader rdr = cmd.ExecuteReader();
                if (rdr.Read())
                {
                    p.Id = rdr.GetInt32(0);
                    p.TypeLien = rdr.GetString(1);
                    p.Origine = rdr.GetInt32(2);
                    p.Destination = rdr.GetInt32(3);
                }
                rdr.Close();
            }
            catch (SQLiteException ex)
            {
                Classe.Log.Logger.Error(ex.Message);
                throw new Exception(String.Format(Classe.SQLite.Messages.SQLite_ERROR_GENERAL, ex.ErrorCode, ex.Message));
            }
            Classe.Log.Logger.Debug("Fin Lien.Charge()");
            return p;
        }

        static public void Delete(int id)
        {
            try
            {
                Classe.Log.Logger.Debug("Debut Lien.Delete(" + id + ")");
                SQLiteCommand cmd = new SQLiteCommand(SQLite.Sql.LIEN_DELETE_ID, SQLite.Sql.GetConnection());

                cmd.Parameters.AddWithValue("@id", id);
                cmd.Prepare();

                Classe.Log.Logger.Debug("Execution requete : " + SQLite.Sql.LIEN_DELETE_ID);
                cmd.ExecuteNonQuery();
            }
            catch (SQLiteException ex)
            {
                Classe.Log.Logger.Error(ex.Message);
                throw new Exception(String.Format(Classe.SQLite.Messages.SQLite_ERROR_GENERAL, ex.ErrorCode, ex.Message));
            }
            catch (Exception ex)
            {
                Classe.Log.Logger.Error(ex.Message);
                throw;
            }
            finally
            {
                Classe.Log.Logger.Debug("Fin LIEN.Delete()");
            }
        }

        static public void Maj(Classe.Lien p)
        {
            try
            {
                Classe.Log.Logger.Debug("Debut Lien.Maj()");
                SQLiteCommand cmd = new SQLiteCommand(SQLite.Sql.LIEN_UPDATE_ID, SQLite.Sql.GetConnection());

                cmd.Prepare();
                cmd.Parameters.AddWithValue("@typelien", p.TypeLien);
                cmd.Parameters.AddWithValue("@origine", p.Origine);
                cmd.Parameters.AddWithValue("@destination", p.Destination);
                cmd.Parameters.AddWithValue("@id", p.Id);
                Classe.Log.Logger.Debug("Execution requete : " + SQLite.Sql.LIEN_UPDATE_ID);
                cmd.ExecuteNonQuery();
            }
            catch (SQLiteException ex)
            {
                Classe.Log.Logger.Error(ex.Message);
                throw new Exception(String.Format(Classe.SQLite.Messages.SQLite_ERROR_GENERAL, ex.ErrorCode, ex.Message));
            }
            finally
            {
                Classe.Log.Logger.Debug("Fin Lien.Maj()");
            }
        }

        static public void Sauve(Classe.Lien p)
        {
            try
            {
                Classe.Log.Logger.Debug("Debut Lien.Sauve()");
                SQLiteCommand cmd = new SQLiteCommand(SQLite.Sql.LIEN_INSERT, SQLite.Sql.GetConnection());

                cmd.Prepare();
                cmd.Parameters.AddWithValue("@typelien", p.TypeLien);
                cmd.Parameters.AddWithValue("@origine", p.Origine);
                cmd.Parameters.AddWithValue("@destination", p.Destination);
                Classe.Log.Logger.Debug("Execution requete : " + SQLite.Sql.PARAM_INSERT);
                cmd.ExecuteNonQuery();
            }
            catch (SQLiteException ex)
            {
                Classe.Log.Logger.Error(ex.Message);
                throw new Exception(String.Format(Classe.SQLite.Messages.SQLite_ERROR_GENERAL, ex.ErrorCode, ex.Message));
            }
            finally
            {
                Classe.Log.Logger.Debug("Fin Lien.Sauve()");
            }
        }
    }
}
